using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.Configuration;
using FAQComponents.Objects;
using FAQComponents.API;
using Adge.UI.WebControls;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for ModerateFAQsCategory.
	/// </summary>
	public partial class ModerateFAQsCategory : ClientSidePage
	{
	
		private int categoryID;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			categoryID = InvariantString.ToInt32(Request.QueryString["ID"]);

			// Make sure that the user is a FAQ Editor for this category
			PermissionAPI.EnsureAdministratorOrFAQEditor(categoryID);

			if (!Page.IsPostBack)
				BindData();
		}

		private void BindData()
		{
			Action = ActionEnum.NotSet;

			dlFAQs.DataSource = FAQAPI.GetFAQPartsNeedingModeration(categoryID);
			dlFAQs.DataBind();

			dlFAQs.Visible = dlFAQs.Items.Count > 0;
			noFAQsToApprove.Visible = !dlFAQs.Visible;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.dlFAQs.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlFAQs_ItemCommand);
			this.dlFAQs.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.dlFAQs_ItemDataBound);

		}
		#endregion

		private void dlFAQs_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
			{
				FAQPart fp = (FAQPart) e.Item.DataItem;
				FAQ f = FAQAPI.GetFAQ(fp.FAQID);

				Label question = (Label) e.Item.FindControl("FAQQuestion");
				question.Text = f.Question;

				if (f.Approved)
					((HyperLink) e.Item.FindControl("lnkViewFAQ")).NavigateUrl = ResolveUrl(string.Format(FAQConfiguration.GetConfig().ShowFAQUrl, f.FAQID));
			}
		}

		private void dlFAQs_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
		{
			int faqPartID = (int) dlFAQs.DataKeys[e.Item.ItemIndex];

			switch (e.CommandName)
			{
				case "Approve":
					FAQAPI.ApproveFAQPart(faqPartID);
					BindData();
					break;
				case "ApproveAndEdit":
					FAQAPI.ApproveFAQPart(faqPartID);
					Response.Redirect(string.Concat("EditFAQ.aspx?ID=", FAQAPI.GetFAQPart(faqPartID).FAQID));
					break;
				case "RequestChanges":
					reasonLabel.Text = "Requested Changes";
					btnSendReason.Text = "Send Requested Changes";
					Action = ActionEnum.RequestChanges;
					FAQPartIDInViewState = faqPartID;
					ShowReasonUI();
					break;
				case "Delete":
					reasonLabel.Text = "Reason for Deletion";
					btnSendReason.Text = "Delete FAQ Part and Send Reason to Submitter";
					Action = ActionEnum.Delete;
					FAQPartIDInViewState = faqPartID;
					ShowReasonUI();
					break;
			}
		}

		private void ShowReasonUI()
		{
			dlFAQs.Visible = false;
			pnlReason.Visible = true;
			reason.Text = string.Empty;
		}

		private void HideReasonUI()
		{
			reason.Text = string.Empty;
			dlFAQs.Visible = true;
			pnlReason.Visible = false;
		}

		protected void btnCancel_Click(object sender, System.EventArgs e)
		{
			HideReasonUI();
		}

		protected void btnSendReason_Click(object sender, System.EventArgs e)
		{
			if (Action == ActionEnum.RequestChanges)
			{
				EmailAPI.SendRequestChangesEmail(FAQPartIDInViewState, reason.Text);

				base.DisplayClientSideAlert("The reason the FAQ has yet to be approved has been sent to the FAQ contributor.");
			}
			else if (Action == ActionEnum.Delete)
			{
				if (reason.Text.Length > 0)
					EmailAPI.SendDeleteNotificationEmail(FAQPartIDInViewState, reason.Text);

				FAQAPI.DeleteFAQPart(FAQPartIDInViewState);

				BindData();
			}
		
			HideReasonUI();
		}

		#region Private Helper Property
		private enum ActionEnum
		{
			NotSet,
			Delete,
			RequestChanges
		}

		private ActionEnum Action
		{
			get
			{
				object o = ViewState["Action"];
				if (o == null)
					return ActionEnum.NotSet;
				else
					return (ActionEnum) o;
			}
			set
			{
				ViewState["Action"] = value;
			}
		}

		private int FAQPartIDInViewState
		{
			get
			{
				object o = ViewState["FAQPartID"];
				if (o == null)
					return -1;
				else
					return (int) o;
			}
			set
			{
				ViewState["FAQPartID"] = value;
			}
		}
		#endregion
	}
}
